package com.zhi.service.impl;

import com.zhi.dao.LeaveRepository;
import com.zhi.entity.ALeave;
import com.zhi.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class LeaveServiceImpl implements LeaveService {
	
	@Autowired
	private LeaveRepository leaveRepository;

	@Override
	public List<ALeave> getEntityList() {
		return leaveRepository.findAll();
	}
	
	@Override
	public List<Object[]> getEntityListWithCodeName() {
		return leaveRepository.getEntityListWithCodeName();
	}
	

	@Override
	public Page<ALeave> getEntityListWithCodeName(Pageable pageable) {
		return leaveRepository.getEntityListWithCodeName(pageable);
	}

	@Override
	public Page<ALeave> getEntityListWithCodeName(Pageable pageable,List<String> studentIds,List<String> stateList){
		return leaveRepository.getEntityListWithCodeName(pageable,studentIds,stateList);
	}

	@Override
	public Page<ALeave> getEntityListWithCodeName2(Pageable pageable,List<Integer> aLeaveIds,List<String> stateList){
		return leaveRepository.getEntityListWithCodeName2(pageable,aLeaveIds,stateList);
	}

	@Override
	public ALeave getOne(Integer id) {
		return leaveRepository.getOne(id);
	}
	
	@Override
	public List<ALeave> findAll(ALeave aLeave) {
		Example<ALeave> example =Example.of(aLeave);
		return leaveRepository.findAll(example);
	}

	@Override
	public ALeave save(ALeave leave) {
		//此处需要使用saveAndFlush，非普通的save，因为需要取得返回对象ID，否则会造成重复插入问题
		return leaveRepository.saveAndFlush(leave);
	}

	@Override
	public void edit(ALeave leave) {
		leaveRepository.save(leave);
		
	}

	@Override
	public void delete(Integer id) {
		leaveRepository.deleteById(id);
	}

	@Override
	public Integer count(ALeave leave) {
		Example<ALeave> example =Example.of(leave);
		return (int) leaveRepository.count(example);
	}

}
